package com.emperor.cashier.app;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.emperor.transaction.logic.Transaction;
import com.swtdesigner.SWTResourceManager;

public class CashierAppDiscountDialog extends Dialog {

	private Text unitPrice;
	private Text specialpriceText;
	public static final String DISCOUNT = "cashier.discount";
	public static final String PRICE = "cashier.price";
	public static final String DIALOG_TRANSACTION = "dialog.transaction";
	public static final String SPECIAL_PRICE = "dialog.specialprice";

	private Combo discountCombo;
	private Transaction dialogTransaction;
	protected Object result;
	protected Shell shell;
	protected Map<String, Object> params;

	private DecimalFormat df0, df2;

	public CashierAppDiscountDialog(Shell parent, int style,
			Map<String, Object> params) {
		super(parent, style);
		this.params = params;
	}

	/**
	 * Create the dialog
	 * 
	 * @param parent
	 * @param style
	 */
	public CashierAppDiscountDialog(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Create the dialog
	 * 
	 * @param parent
	 */
	public CashierAppDiscountDialog(Shell parent) {
		this(parent, SWT.NONE);
	}

	/**
	 * Open the dialog
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}

	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {

		df0 = new DecimalFormat();
		df0.applyPattern("##########.##");
		df0.setMinimumFractionDigits(0);
		df0.setMaximumFractionDigits(0);

		df2 = new DecimalFormat();
		df2.applyPattern("##########.##");
		df2.setMinimumFractionDigits(2);
		df2.setMaximumFractionDigits(2);

		dialogTransaction = (Transaction) getParams().get(DIALOG_TRANSACTION);

		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
		shell.setBackgroundImage(SWTResourceManager.getImage(
				CashierAppDiscountDialog.class, "/img/main.jpg"));
		shell.setImage(SWTResourceManager.getImage(
				CashierAppDiscountDialog.class, "/img/iMac OSX.png"));
		shell.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));
		shell.setSize(500, 375);
		shell.setText("Discount Dialog");

		final Composite composite = new Composite(shell, SWT.NONE);
		composite.setBounds(0, 0, 494, 343);

		final Label discountLabel = new Label(composite, SWT.NONE);
		discountLabel.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));
		discountLabel.setText("Discount %");
		discountLabel.setBounds(51, 156, 85, 20);

		final Button okButton = new Button(composite, SWT.NONE);
		okButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (!StringUtils.isEmpty(specialpriceText.getText())) {
					Double specialprice = new Double(0.0);
					Double discount = new Double(0.0);
					try {
						specialprice = new Double(specialpriceText.getText());
					} catch (Exception nfex) {
					}

					discount = (specialprice / dialogTransaction.getPrice()) * 100;
					discount = new Double(Math.round(discount));

					getParams().put(SPECIAL_PRICE, specialpriceText.getText());
					getParams().put(DISCOUNT, discount.toString());
					getShell().close();
				} else if (!StringUtils.isEmpty(discountCombo.getText())) {
					getParams().put(DISCOUNT, discountCombo.getText());
					getShell().close();
				}
			}
		});
		okButton.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));
		okButton.setImage(SWTResourceManager.getImage(
				CashierAppDiscountDialog.class, "/img/Symbol-Check.png"));
		okButton.setText("OK");
		okButton.setBounds(174, 288, 119, 30);

		discountCombo = new Combo(composite, SWT.NONE);
		discountCombo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				updatePriceText();
			}
		});
		discountCombo.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));
		discountCombo.setBounds(200, 153, 226, 28);

		discountCombo.add("10");
		discountCombo.add("15");
		discountCombo.add("20");
		discountCombo.add("25");
		discountCombo.add("30");

		final Label customerDiscountWillLabel = new Label(composite, SWT.NONE);
		customerDiscountWillLabel.setFont(SWTResourceManager.getFont("", 12,
				SWT.NONE));
		customerDiscountWillLabel
				.setText("Customer Discount will always overwrite this discount");
		customerDiscountWillLabel.setBounds(51, 114, 375, 20);

		final Label discountLabel_1 = new Label(composite, SWT.NONE);
		discountLabel_1.setBounds(51, 233, 95, 20);
		discountLabel_1.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));
		discountLabel_1.setText("Special Price");

		specialpriceText = new Text(composite, SWT.BORDER);
		specialpriceText.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));
		specialpriceText.setBounds(200, 230, 226, 30);

		final Label discountLabel_1_1 = new Label(composite, SWT.NONE);
		discountLabel_1_1.setBounds(51, 197, 85, 20);
		discountLabel_1_1.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));
		discountLabel_1_1.setText("Unit Price");

		unitPrice = new Text(composite, SWT.READ_ONLY | SWT.BORDER);
		unitPrice.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));
		unitPrice.setBounds(200, 187, 226, 30);
		unitPrice.setText(df2.format(dialogTransaction.getPrice()));

		//
		centreShell(shell);
	}

	public void centreShell(Shell shell) {
		Rectangle pDisplayBounds = shell.getDisplay().getBounds();
		Rectangle pShellBounds = shell.getBounds();

		int nLeft = (pDisplayBounds.width - pShellBounds.width) / 2;
		int nTop = (pDisplayBounds.height - pShellBounds.height) / 2;

		shell.setBounds(nLeft, nTop, pShellBounds.width, pShellBounds.height);
	}

	public Map<String, Object> getParams() {
		return params;
	}

	public void setParams(Map<String, Object> params) {
		if (params != null) {
			this.params = params;
		}
	}

	public Shell getShell() {
		return shell;
	}

	private void updatePriceText() {
		if (!StringUtils.isEmpty(discountCombo.getText())) {
			Double discount = new Double(10.0);
			try {
				discount = new Double(discountCombo.getText());
			} catch (Exception nfex) {
			}

		}
	}
}
